package com.atzon.controller;

import com.alibaba.fastjson.JSON;
import com.atzon.service.ArtService;
import com.atzon.service.impl.ArtServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Map;

@WebServlet("/art/*")
public class ArtServlet extends BaseServlet {
    private ArtService artService = new ArtServiceImpl();
    public void list(HttpServletRequest request, HttpServletResponse response){
        try {
            String currentPage = request.getParameter("currentPage");
            String search = request.getParameter("search");
            int page = 1;
            if(null != currentPage){
                page = Integer.parseInt(currentPage);
            }
            if (null != search) {
                search = URLEncoder.encode(search, "utf-8");
                response.sendRedirect(request.getContextPath() + "/view/article?page=" + page + "&search=" + search);
            } else {
                response.sendRedirect(request.getContextPath() + "/view/article?page=" + page);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    public void favList(HttpServletRequest request, HttpServletResponse response){
        try {
            String currentPage = request.getParameter("currentPage");
            String search = request.getParameter("search");
            int page = 1;
            if(null != currentPage){
                page = Integer.parseInt(currentPage);
            }
            if (null != search) {
                search = URLEncoder.encode(search, "utf-8");
                response.sendRedirect(request.getContextPath() + "/view/artCollect?page=" + page + "&search=" + search);
            } else {
                response.sendRedirect(request.getContextPath() + "/view/artCollect?page=" + page);
            }
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    public void addMyArt(HttpServletRequest req, HttpServletResponse resp){
        try {
            String title = req.getParameter("title");
            String content = req.getParameter("content");
            Map<String, Object> userBaseInfo = (Map<String, Object>)req.getSession().getAttribute("userBaseInfo");
            String username = (String)userBaseInfo.get("username");
            Integer userId = (Integer)userBaseInfo.get("id");
            artService.addArt(title,content,username,userId);
            resp.sendRedirect(req.getContextPath() + "/view/artAdd");
        } catch (Exception e){
            e.printStackTrace();
        }
    }
    public void delFav(HttpServletRequest req, HttpServletResponse resp){
        try {
            Map<String, Object> userBaseInfo = (Map<String, Object>)req.getSession().getAttribute("userBaseInfo");
            Integer userId = (Integer)userBaseInfo.get("id");
            String aId = req.getParameter("aId");
            artService.delFavById(userId,Integer.parseInt(aId));
            String jsonData = JSON.toJSONString(null);
            resp.getWriter().write(jsonData);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
